package com.hnjk.cbts;

import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

public class Appender extends AppenderBase<ILoggingEvent> {
	private PatternLayout patternLayout;
	
	@Override
    public void start() {
        patternLayout = new PatternLayout();
        patternLayout.setContext(getContext());
        patternLayout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
        patternLayout.start();

        super.start();
    }

	@Override
	protected void append(ILoggingEvent eventObject) {
		String formattedMsg = patternLayout.doLayout(eventObject);
		SimpleAttributeSet att = new SimpleAttributeSet();
		att.addAttribute(StyleConstants.CharacterConstants.Foreground, new Color(0, 0, 0));
		JTextPane textPane = GUIStarter.mFrame.geTextPane();
		Document document = textPane.getDocument();
    	try {
    		document.insertString(document.getLength(), formattedMsg, att);
    	} catch (BadLocationException e) {
			e.printStackTrace();
		}   		
	}
}
